package com.nageoffer.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nageoffer.shortlink.project.dao.entity.LinkLocaleStatsDO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * ClassName: LinkLocaleStatsMapper
 * Package:
 * Description:  地区统计访问持久层
 *
 * @Author:xiaohei
 * @Create 2024/6/12 8:38
 * Version 1.0
 */
public interface LinkLocaleStatsMapper extends BaseMapper<LinkLocaleStatsDO> {
    /**
     * 记录地区监控访问数据
     * @param linkLocaleStatsDO
     */
    @Insert("INSERT INTO t_link_locale_stats (full_short_url, gid, date, cnt,country,province,city,adcode, create_time, update_time, del_flag) " +
            "VALUES (" +
            "    #{linkLocalStats.full_short_url}," +
            "    #{linkLocalStats.gid}," +
            "    #{linkLocalStats.date}," +
            "    #{linkLocalStats.cnt}," +
            "    #{linkLocalStats.country}," +
            "    #{linkLocalStats.province}," +
            "    #{linkLocalStats.city}," +
            "    #{linkLocalStats.adcode}," +
            "    NOW()," +
            "    NOW()," +
            "    0" +
            ")" +
            "ON DUPLICATE KEY UPDATE" +
            "    cnt = cnt + #{linkLocalStats.cnt};")
    void shortLinkLocaleStats(@Param("linkLocalStats") LinkLocaleStatsDO linkLocaleStatsDO);

    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    province, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    t_link_locale_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, province;")
    List<LinkLocaleStatsDO> listLocaleByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);
}
